package com.shenhan.lock8;

import lombok.SneakyThrows;

import java.util.concurrent.TimeUnit;

/**
 * @author shenhan
 * @create 2020/6/10 15:52
 * @description 8锁，就是关于锁的8个问题
 * 3、增加一个普通方法后，两个线程先打印发短信还是hello？
 */
public class Test2 {
    public static void main(String[] args) throws InterruptedException {
        Phone2 phone1 = new Phone2();
        Phone2 phone2 = new Phone2();
        new Thread(phone1::sendMessage, "A").start();
        TimeUnit.SECONDS.sleep(1L);
        new Thread(phone2::call, "B").start();
    }
}

class Phone2 {
    @SneakyThrows
    public synchronized void sendMessage() {
        TimeUnit.SECONDS.sleep(4L);
        System.out.println(Thread.currentThread().getName() + " 发短信");
    }

    public synchronized void call() {
        System.out.println(Thread.currentThread().getName() + " 打电话");
    }

    public void hello() {
        System.out.println("Hello");
    }
}
